using System;
using System.Collections.Generic;
using Castle.Components.Validator;
using Castle.MonoRail.Framework;
using Castle.MonoRail.Framework.Helpers;
using Query;
using Rhino.Commons;
using WineCellar.Data;
using WineCellar.Web.Filters;

namespace WineCellar.Web.Controllers
{
    [Layout("index"), Rescue("generalerror"),AuthenticationFilter()]        
    public class CellarWinesController : ApplicationController
    {               
        public void Index()
        {
        }
        public void Search()
        {
            if (IsAjaxRequest) { CancelLayout(); }            
        }
        public void List(string name)
        {
            if (IsAjaxRequest) { CancelLayout(); }
            PropertyBag["name"] = name;
			Cellar cellar = Repository<Cellar>.FindOne(Where.Cellar.User.UserId.Eq(GetLoginMemberShipUser().ProviderUserKey));
			PropertyBag["cellarwines"] = PaginationHelper.CreatePagination(this.Context, Repository<CellarWine>.FindAll(Where.CellarWine.Cellar.Eq(cellar) && Where.CellarWine.Wine.Name.Like(name), OrderBy.CellarWine.Price.Desc), 10);                      
        }        
        public void New(int id)
        {
            if (IsAjaxRequest) { CancelLayout(); }
			Cellar cellar = Repository<Cellar>.FindOne(Where.Cellar.User.UserId.Eq(GetLoginMemberShipUser().ProviderUserKey));
			ICollection<CellarWine> cellarwines = Repository<CellarWine>.FindAll(
														Where.CellarWine.Wine.Id.Eq(id)&& 
														Where.CellarWine.Cellar.Eq(cellar));
            if (cellarwines.Count <= 0)                
            {
				Wine wine = Repository<Wine>.Load(id);
                PropertyBag["wine"] = wine;
                Session["wine"] = wine;
            	PropertyBag["cellar"] = cellar;
                PropertyBag["cellarwine"] = new CellarWine();                
            }
            else
            {
                ErrorSummary summary = new ErrorSummary();
                summary.RegisterErrorMessage("CellarWine", "Wine is already added in your cellar.");                
                Flash["summary"] = summary;                
            }                      
        }       
        public void Create([DataBind("cellarwine",Validate = true)]CellarWine cellarWine)
        {
            if (!HasValidationError(cellarWine))
            {
                Wine wine = FindWine();
				Cellar cellar = Repository<Cellar>.FindOne(Where.Cellar.User.UserId.Eq(GetLoginMemberShipUser().ProviderUserKey));
                cellarWine.Cellar = cellar;
                cellarWine.Wine = wine;
                cellarWine.AddedBy = GetLoginUser();
                cellarWine.DateAdded = DateTime.Now;
				With.Transaction(delegate
									{
										Repository<CellarWine>.SaveOrUpdate(cellarWine);
									});                  
                ShowAll();
            }
            else
            {
                Flash["cellarwine"] = cellarWine;
                Flash["summary"] = GetErrorSummary(cellarWine);                
            }
        }
        public void Edit(int id)
        {
            if (IsAjaxRequest) { CancelLayout(); }
			CellarWine cellarWine = Repository<CellarWine>.Load(id);
            PropertyBag["cellarWine"] = cellarWine;
            Session["wine"] = cellarWine.Wine;           
        }
        public void Delete(int id)
        {
			CellarWine cellarWine = Repository<CellarWine>.Load(id);
            PropertyBag["deletedObject"] = cellarWine;
			With.Transaction(delegate
			                 	{
									Repository<CellarWine>.Delete(cellarWine);  	
			                 	});			          
            RenderView("shared","delete");
        }
        public void Update([DataBind("cellarwine",Validate = true)] CellarWine cellarWine)
        {
            if (!HasValidationError(cellarWine))
            {
                cellarWine.Wine = FindWine();
				cellarWine.Cellar = Repository<Cellar>.FindOne(Where.Cellar.User.UserId.Eq(GetLoginMemberShipUser().ProviderUserKey));
				With.Transaction(delegate
				                 	{
										Repository<CellarWine>.SaveOrUpdate(cellarWine);                	
				                 	});            	
                PropertyBag["updatedObject"] = cellarWine;
            }
            else
            {
                Flash["updatedObject"] = cellarWine;
                Flash["summary"] = GetErrorSummary(cellarWine);                
            }
			RenderView("shared", "update");
        }
        public void View(int id)
        {
            if (IsAjaxRequest) { CancelLayout(); }
            PropertyBag["cellarwine"] = Repository<CellarWine>.Load(id);            
        }
        public void ShowAll()
        {
          List("%");
          RenderView("list");
        }

        #region private methods        
        
		private Wine FindWine()
        {
            if (Session["wine"] == null)
            {
                Session["wine"] = new Wine();
            }
            return (Wine)Session["wine"];
        }
        #endregion
    }
}
